// @ts-ignore
import Cookies from 'js-cookie'
// 通用功能函数
export default function useTools () {
  /** 设置setSessionStorage */
  function setSessionStorage (key: string, val: string): void {
    sessionStorage.setItem(key, val)
  }

  /** 获取sessionStorage */
  function getSessionStorage (key: string):string {
    return sessionStorage.getItem(key) as string
  }

  /** 清除sessionStorage */
  function cleanSessionStorage (key: string) {
    sessionStorage.removeItem(key)
  }

  /** 清除所有sessionStorage */
  function cleanAllSessionStorage () {
    sessionStorage.clear()
  }

  /** 设置setLocalStorage */
  function setLocalStorage (key: string, val: string): void {
    localStorage.setItem(key, val)
  }

  /** 获取localStorage */
  function getLocalStorage (key: string) {
    localStorage.getItem(key)
  }

  /** 清除localStorage */
  function cleanLocalStorage (key: string) {
    localStorage.removeItem(key)
  }

  /** 清除所有localStorage */
  function cleanAllLocalStorage () {
    localStorage.clear()
  }

  /** 描述： set Cookies by 1 day */
  function setCookies (key: string, value: string | object) {
    Cookies.set(key, value, { expires: 1 }) // default expires time 1day
  }

  /** 描述： get Cookies */
  function getCookies (key: string):string|object {
    return Cookies.get(key)
  }

  /** 描述： remove Cookies */
  function removeCookies (key:string):void {
    Cookies.remove(key)
  }
  return {
    setSessionStorage,
    getSessionStorage,
    cleanSessionStorage,
    cleanAllSessionStorage,
    setLocalStorage,
    getLocalStorage,
    cleanLocalStorage,
    cleanAllLocalStorage,
    setCookies,
    getCookies,
    removeCookies
  }
}
